package com.xjrsoft.common.serializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.xjrsoft.core.tool.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;

import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;

@Slf4j
public class BlobDeserializer  extends JsonDeserializer<Blob> {
    @Override
    public Blob deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String value = p.getValueAsString();
        if (!StringUtil.isEmpty(value)) {
            try {
                return new SerialBlob(value.getBytes("UTF-8"));
            } catch (SQLException e) {
                log.error("反序列化Blob类型字段失败！", e);
            }
        }
        return null;
    }
}
